﻿namespace My.Hydrator.Conventions
{
    using System;
    using My.Hydrator.Helpers;

    public class CommonTypeConvention<TType> : BaseTypeConvention, ITypeConvention
    {
        private readonly CommonType commonType;

        public CommonTypeConvention(CommonType commonType)
        {
            this.commonType = commonType;
        }

        public Type Type
        {
            get { return typeof(TType); }
        }

        public object GenerateValue(object[] customAttributes)
        {
            string result;
            int index;

            switch (this.commonType)
            {
                case CommonType.AmericanName:
                    result = Lookups.AmericanNames[this.Random.Next(0, Lookups.AmericanNames.Count - 1)];
                    index = Lookups.AmericanNames.IndexOf(result);
                    break;
                case CommonType.AmericanLastName:
                    result = Lookups.AmericanLastNames[this.Random.Next(0, Lookups.AmericanLastNames.Count - 1)];
                    index = Lookups.AmericanLastNames.IndexOf(result);
                    break;
                case CommonType.AmericanState:
                    result = Lookups.AmericanStates[this.Random.Next(0, Lookups.AmericanStates.Count - 1)];
                    index = Lookups.AmericanStates.IndexOf(result);
                    break;
                case CommonType.AmericanCity:
                    result = Lookups.AmericanCities[this.Random.Next(0, Lookups.AmericanCities.Count - 1)];
                    index = Lookups.AmericanCities.IndexOf(result);
                    break;
                case CommonType.Sex:
                    result = Lookups.Sex[this.Random.Next(0, Lookups.Sex.Count - 1)];
                    index = Lookups.Sex.IndexOf(result);
                    break;
                default:
                    throw new NotSupportedException();
            }

            if (typeof(TType) == typeof(string))
            {
                return result;
            }
            else if (typeof(TType) == typeof(int))
            {
                return index;
            }
            else
            {
                throw new NotSupportedException();
            }
        }
    }
}
